<?php
	require_once "../includes/pagination.php";
	
	$itemPerPage=10;
	
	if(isset($_POST['btnSearchPro'])) {
		$searchKey = $_POST['searchkey'];		
		$searchBy = $_POST['searchby'];		
		$date = $_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
		if(!empty($_POST['postedBy'])){
			
		$sqlSelPro = "SELECT tbl_product.*, tbl_users.username, tbl_users.user_id 
						  FROM tbl_product, tbl_users 
						  WHERE tbl_users.username LIKE '%".$_POST['postedBy']."%' AND
						  tbl_users.user_id=tbl_product.posted_by";
						  
		if(!empty($_POST['productName']))
			$sqlSelPro .= " AND tbl_product.product_name LIKE '%".$_POST['productName']."%'";			        
		if(isset($_POST['priceFrom']) && !empty($_POST['priceTo']))
			$sqlSelPro .= " AND tbl_product.product_price BETWEEN ".$_POST['priceFrom']." AND ".$_POST['priceTo'];
		if($_POST['dYear']!="" && $_POST['dMonth']!="" && $_POST['dDay']!="") {
		 		$date=$_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
				$sqlSelPro .= " AND tbl_product.posted_date LIKE '$date%'";				
		 }	  
		} 
		else if(!empty($_POST['productName']) || (isset($_POST['priceFrom']) || !empty($_POST['priceTo'])) || (!empty($_POST['dYear'])&& !empty($_POST['dMonth']) && !empty($_POST['dDay']))){
			$sqlSelPro = "SELECT * FROM tbl_product WHERE ";		
		if(!empty($_POST['productName']))
			$sqlSelPro .= "product_name LIKE '%".$_POST['productName']."%' AND ";			        
		if(!empty($_POST['priceFrom']) && !empty($_POST['priceTo']))
			$sqlSelPro .= "product_price BETWEEN ".$_POST['priceFrom']." AND ".$_POST['priceTo']."  AND";
		else if(!empty($_POST['priceFrom']))
			$sqlSelPro .= "product_price >=".$_POST['priceFrom']."  AND";
		else if(!empty($_POST['priceTo']))
			$sqlSelPro .= "product_price <=".$_POST['priceTo']."  AND";
		if($_POST['dYear']!="" && $_POST['dMonth']!="" && $_POST['dDay']!="") {
		 		$date=$_POST['dYear']."-".$_POST['dMonth']."-".$_POST['dDay'];
				$sqlSelPro .= "posted_date LIKE '$date%' AND ";				
		 }	  
	$count=strlen($sqlSelPro);
        $sqlSelPro=substr($sqlSelPro,0,($count-4));
	}	
	else {
		if(!empty($_GET['sort']))
		{
			$sqlSelPro="SELECT * FROM tbl_product";
		}
		else{
			$sqlSelPro="SELECT * FROM tbl_product ORDER BY product_id DESC";
		}
	}
	} 
	else {
		if(!empty($_GET['sort']))
		{
			$sqlSelPro="SELECT * FROM tbl_product";
		}
		else{
			$sqlSelPro="SELECT * FROM tbl_product ORDER BY product_id DESC";
		}
	}
	
	
	$sort = '';
	$ut = $u = $fn = $ln = $dg = 'asc';
	if(isset($_GET['sort']))
	{
	 if($_GET['sort']=='homeprod')
	 {
	  	$sqlSelPro.=' ORDER BY show_on_home DESC';
	 }
	 else if($_GET['sort']=='sponsered')
	 {
	        $sqlSelPro.=' ORDER BY show_on_panel DESC';	 
         }
	}
	
?>
<script src="<?php echo $base_url?>js/jquery-1.4.4.js" type="text/javascript"></script>
<script>
function onlyint(event) {
	
	var charCode = event.keyCode || event.which;
	if(event.shiftKey && charCode == 9)
	{return true}
	else  if (event.shiftKey)
	{ $('#digit_err').show().fadeOut("slow"); return false; }
	else if  (((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 46)) )
	{ return true; } else { $('#digit_err').show().fadeOut("slow"); return false; }
}

function show_on_home_page_toggle(id)
{
	var status = 1;
	
	if($("#show_on_home_"+id).is(":checked")==false)
	status = 0;
	
	var url = "<?php echo $base_url;?>admin/ajax_show_pro_on_home.php";
	$.post(url,
	{ 'product_id': id,'status':status },
	function(data)
	{
		if(data=='fail')
			alert('Something went wrong.');
	},'html');
}
function show_on_panel_toggle(id)
{	
	var status = 1;
	
	if($("#show_on_panel_"+id).is(":checked")==false)
	status = 0;
	
	var url = "<?php echo $base_url;?>admin/ajax_show_pro_on_panel.php";
	$.post(url,
	{ 'product_id': id,'status':status },
	function(data)
	{
		if(data=='fail')
			alert('Something went wrong.');
	},'html');
}
</script>
<style type="text/css">
	div#list-sort { padding:10px; text-align:right; }
	div#suc-msg { padding:10px; border:1px solid #CCC; color:#0000FF; }
	div#err-msg { padding:10px; border:1px solid #CCC; color:#FF0000; }
</style>		  
<table width="100%"  border="1" cellspacing="0" class="container">
	<tr><td width="469" height="25" class="tdHeader">Selling Products</td></tr>
	<tr>
		<td height="577">
			<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr><td height="10"></td><td></td><td></td><td></td><td></td></tr>
					<tr>
						<td width="2%" height="31"></td>
						<td width="16%">Product name</td>
						<td width="30%"><input type="text" name="productName" id="productName" style="width:157px;"  /></td>
						<td width="13%">Posted by</td>
						<td width="39%"><input type="text" name="postedBy" id="postedBy" style="width:215px;" /></td>
					</tr>
					<tr>
						<td height="34">&nbsp;</td>
						<td>Price range</td>
						<td>
							<input type="text" name="priceFrom" id="priceFrom" style="width:67px;" onkeydown='return onlyint(event);' /> &nbsp;&nbsp;
							<input type="text" name="priceTo" id="priceTo" style="width:67px;" onkeydown='return onlyint(event);' />
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<font color="gray">From</font>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<font color="gray">To</font><br />
							<div style="text-align: center; color: red; display: none;" id="digit_err">Digits  Only</div>
							<?php /*
							<select name="priceFrom" id="priceFrom" style="width:78px;">
								<option value="">From</option>
								<?php
								$i=0;
								for($i=0; $i<=5000; $i+=100) {
								echo "<option value='$i'> $$i </option>";
								}
								?>
							</select>
							    
							
							
							<select name="priceTo" id="priceTo"  style="width:78px;">
								<option value="">To</option>
								<?php
								$i=100;
								for($i=100; $i<=5000; $i+=100) {
								echo "<option value='$i'> $$i </option>";
								}
								?>
							</select>  */
							?>
						</td>
						<td>Posted date</td>
						<td>
							<select name="dMonth" id="dMonth" style="width:80px">
								<option value="">..Month..</option>
								<option value="01">January</option>
								<option value="02">February</option>
								<option value="03">March</option>
								<option value="04">April</option>
								<option value="05">May</option>
								<option value="06">June</option>
								<option value="07">July</option>
								<option value="08">August</option>
								<option value="09">September</option>
								<option value="10">October</option>
								<option value="11">November</option>
								<option value="12">December</option>
							</select>
							<select name="dDay" id="dDay" style="width:80px">
								<option value="">..Day..</option>
								<?php
								for($i=1;$i<=31;$i++) {
									if($i<=9)
									$date = '0'.$i;
									else
									$date = $i;
									?>
									<option value="<?php echo $date;?>"><?php echo $date;?></option>
									<?php
								}
								?>
								
							</select>
							<input type="dYear" name="dYear" id="emailAgain3" value="YYYY" onclick="this.value=''" size="4" maxlength="4" />
						</td>
					</tr>
					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Product" /></td>
					</tr>
				</table>
			</form>
			<br>
			<form name="frmListPro" id="frmListPro" method="post" action="">
				<input type="hidden" name="keyword" id="keyword">
				<?php
				if(isset($_POST['btnDelete'])) {
					//echo count($_POST['chk_product']); exit;
					if(count($_POST['chk_product']) > 0) {
						$i=0;
						foreach($_POST['chk_product'] as $pId) {
							
							$imgPaths=getImagePath($pId,'1');
							
							$sql="DELETE FROM tbl_product where  product_id='$pId'";
							$rst=mysql_query($sql);
							if($rst) { 						
								$i=$i+1;
								//if($imgPaths[0]!="") { unlink("../".$imgPaths[0]); }
								//if($imgPaths[1]!="") { unlink("../".$imgPaths[1]); }
								//if($imgPaths[2]!="") { unlink("../".$imgPaths[2]); }
							}
						}
						if($i>0) { echo "<div id='suc-msg'> $i item(s) deleted. </div><br>"; }
					}
					else {
						echo "<div id='err-msg'> Select Item(s). </div><br>";
					}
				}
				?>
				<table width='100%' border='0' align='center' cellspacing='0'>
				<tr>
					  <td colspan="7">
						 <b>Sort  By :</b>
						 <a href="<?php echo $base_url;?>admin/index.php?listpro&sort=homeprod">Show on Homepage</a> &nbsp;&nbsp;&nbsp;<a href="<?php echo $base_url;?>admin/index.php?listpro&sort=sponsered">Show on Sponsored deals</a>
					  </td>
				   </tr>
					<tr>
						<td colspan="7" style="color:#7084C9; font-weight:bold; text-align:center;">Products which are checked will be displayed at home page randomly.</td>
					</tr>
					<tr class="InnerHeader">
						<td width='22%' class="line" colspan="2">&nbsp;</td>
						<td width='19%' class="line"><strong>Product name </strong></td>
						<td width='16%' class="line"><strong>Posted by </strong></td>
						<td width='19%' class="line"><strong>Posted date</strong> </td>
						<td width='10%' class="line"><strong>Price</strong> </td>
						<td width='14%' class="line"><strong>Show on Homepage / Sponsered</strong> </td>
					</tr>   

					<?php
					//echo $sqlSelPro;
					$rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));				 
				  
					$num=mysql_num_rows($rstSelPro);
				   
					if($num==0)
					{
						echo "<tr> <td colspan='7'>No Products Found...</td></tr>";
					}
					else {
						
						while($rowSelPro=mysql_fetch_array($rstSelPro))
						{
							$productId=$rowSelPro['product_id'];
							$productName=wordFormater($rowSelPro['product_name'],10);									
							$title=$rowSelPro['product_name'];
							$productPrice=number_format($rowSelPro['product_price'],2);
							$pimage = getProductImage($productId,'p');
							if($pimage['thumb']!="") {
						   		$productThumb = "../".$pimage['thumb'];
							} else {
						   		$productThumb="../images/no_image.gif";	
							}
							$postedDate = $rowSelPro['posted_date'];
							//get username
							$user=$rowSelPro['posted_by'];
							$username = ucfirst(getUser($user));
						   
							//$pDate=strftime(" %d, %Y", strtotime($postedDate));
							$pDate=date('M d Y',strtotime($postedDate));
							$soh_check=$sop_check='';
							$show_on_home = $rowSelPro['show_on_home'];
							if($show_on_home==1)
							$soh_check='checked';
							
							$show_on_panel = $rowSelPro['show_on_panel'];
							if($show_on_panel==1)
							$sop_check='checked';
						   					   
							echo "<tr>
						   		<td style='vertical-align:middle;'> <input type='checkbox' name='chk_product[]' value='$productId'> </td>
						        <td height='106'>
						   			<div align='center'>
										<a href='index.php?listpro=$productId' style='text-decoration: none; color:#FFF;'><img src='$productThumb'></a>
									</div>
								 </td>								 
								 <td style='vertical-align:middle;'> 
								 	<a href='index.php?listpro=$productId' title='$title'> $productName </a> 
								 </td>
								 <td style='vertical-align:middle;'> <a href='index.php?members=$user'> $username </a> </td>
								 <td style='vertical-align:middle;'> $pDate </td>
								 <td style='vertical-align:middle;'> $$productPrice </td>
								 <td style='vertical-align:middle;'><input title='Toggle Show on Home Page' type='checkbox' $soh_check onclick='show_on_home_page_toggle($productId);' name='show_on_home_$productId' id='show_on_home_$productId' /> &nbsp; <input type='checkbox' $sop_check onclick='show_on_panel_toggle($productId);' name='show_on_panel_$productId' id='show_on_panel_$productId' title='Toggle Show in Sponsered List' /></td>
							</tr>";	
							echo "<tr>
									 <td colspan='7'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
								  </tr>";   					  
						 	
						}	
						echo "<tr>
									 <td colspan='7'> 
									 	<div style='border-top:1px solid #CCCCCC;'>
									 		<input type='submit' value='Delete' name='btnDelete'>
									 	</div> 
									 </td>
								  </tr>"; 					
					} 				
						?>
					<tr>
						<td colspan="6">
							<div align="center"><?php
								$link="listpro";
								echo $disp=getPagingLink($sqlSelPro,$itemPerPage,$link);							   
								?>
							</div>
						</td>
					</tr>
				</table>
			</form>
		</td>
	</tr>
</table>